<template>
  <el-row type="flex" justify="space-between" align="middle" style="height: 40px; width: 100%;">
    <el-col :span="20">
      <i v-if="isRoot" class="el-icon-phone" />
      <span>{{ nodeData.name }}</span>
    </el-col>
    <el-col :span="4">
      <el-row>
        <el-col :span="12">{{ nodeData.manager }}</el-col>
        <el-col :span="12">
          <el-dropdown @command="handleCommand">
            <span @click.stop>操作<i class="el-icon-arrow-down" /></span>
            <template #dropdown>
              <el-dropdown-menu>
                <el-dropdown-item command="add">添加子部门</el-dropdown-item>
                <el-dropdown-item v-if="!isRoot" command="edit">查看部门</el-dropdown-item>
                <el-dropdown-item v-if="!isRoot" command="del">删除部门</el-dropdown-item>
              </el-dropdown-menu>
            </template>
          </el-dropdown>
        </el-col>
      </el-row>
    </el-col>
  </el-row>
</template>

<script>
import { delDepartment } from '@/api/departments'
export default {
  name: 'TreeTools',
  props: {
    // 接受父组件的所有部门.
    nodeData: {
      type: Object,
      required: true
    },

    // 操作显示几个
    isRoot: {
      type: Boolean,
      default: false
    }
  },

  methods: {
    // element-ui 自带的下拉菜单点击事件
    // 删除部门
    handleCommand(command) {
      console.log(command)
      switch (command) {
        case 'add':
          this.add()
          break
        case 'edit':
          this.edit()
          break
        case 'del':
          this.del()
          break
      }
    },

    add() {
      console.log('增加')
      // 让父组件显示弹窗，并且需要携带nodeData(点谁数谁就加)
      this.$emit('addDepart', this.nodeData)
    },

    edit() {
      console.log('修改')
      // 显示弹窗
      this.$emit('editDepart', this.nodeData)
    },

    async del() {
      console.log('删除')
      try {
        await this.$confirm('你确定要删除该部门吗？', '⚠', {
          type: 'warning'
        })
      } catch (error) {
        return
      }
      // 删除部门
      await delDepartment(this.nodeData.id)
      // 子传父，通知父组件重新渲染部门
      this.$emit('getDepartments')
      this.$message.success('删除成功')
    }
  }
}
</script>

<style  >

</style>
